//
//  HMLeftViewController.m
//  仿真开发
//
//  Created by B.HOPE on 2017/3/22.
//  Copyright © 2017年 B.HOPE. All rights reserved.
//

#import "HMLeftViewController.h"
#import <Masonry.h>
#import <YYModel.h>
#import "HMLeftMainCell.h"
#import "HMLeftMainModel.h"
#import "HMMyMessageController.h"
#import "EnterView.h"
#import "HMSetAndHelpController.h"
#import "EnterMsg.h"
#import "HMLoginModel.h"

@interface HMLeftViewController () <UITableViewDelegate,UITableViewDataSource,EnterViewDelegate,HMSetAndHelpControllerDelegate>

@property (nonatomic, strong) UITableView *tabView;

/**
 主页面数据
 */
@property (nonatomic, strong) NSArray *array;

/**
 跳转的VIEW
 */
@property (nonatomic, strong) NSArray *arrayVC;
@property (nonatomic, strong) NSArray *arrayVCTitle;


@property (nonatomic, strong) NSMutableDictionary *dict;

@property (nonatomic, strong) UIImageView *icon;
@property (nonatomic, strong) UIButton *registerBtn;

@property (nonatomic, strong) UIButton *iconBtn;
@property (nonatomic, strong) UILabel *nameLabel;

@property (nonatomic, strong)NSString *enter;

@end

static NSString *leftCell = @"cell";

@implementation HMLeftViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
    [self setupUI];
    
    _arrayVC = @[@"HMReputate",@"HMCaseManageController",@"HMDocListController",@"HMSetAndHelpController"];
    _arrayVCTitle = @[@"名医申请",@"病例管理",@"关注医生",@"设置与帮助"];
    
    self.view.backgroundColor = [UIColor colorWithRed:77/255.0 green:194/255.0 blue:156/255.0 alpha:1];
    NSString *entPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]stringByAppendingPathComponent:@"enter.h"];
    EnterMsg *msg = [NSKeyedUnarchiver unarchiveObjectWithFile:entPath];
    _enter = msg.enter;
    
}

- (void)setupUI
{
    
    NSString *entPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]stringByAppendingPathComponent:@"enter.h"];
    EnterMsg *msg = [NSKeyedUnarchiver unarchiveObjectWithFile:entPath];
    _enter = msg.enter;

    [self setHead];
    
    _tabView = [[UITableView alloc] init];
    _tabView.delegate = self;
    _tabView.dataSource = self;
    _tabView.separatorStyle = UITableViewScrollPositionNone;
    _tabView.backgroundColor = [UIColor colorWithRed:77/255.0 green:194/255.0 blue:156/255.0 alpha:1];
    [self.view addSubview:_tabView];
    [_tabView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(200);
        make.left.right.bottom.offset(0);
    }];
    _tabView.hidden = YES;
    
    [_tabView registerClass:[HMLeftMainCell class] forCellReuseIdentifier:leftCell];
    _tabView.estimatedRowHeight = 100;
}

- (void)setHead
{
    if (_dict == nil)
    {
        _iconBtn.hidden = YES;
        _nameLabel.hidden = YES;
        _tabView.hidden = YES;
        
        _icon = [[UIImageView alloc] init];
        _icon.image = [UIImage imageNamed:@"doctor_defaultphoto_male"];
        _icon.layer.cornerRadius = 25;
        _icon.layer.masksToBounds = YES;
        [self.view addSubview:_icon];
        [_icon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(80);
            make.left.offset(100);
            make.size.mas_offset(CGSizeMake(50,50));
        }];
        
        _registerBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [_registerBtn setTintColor:[UIColor whiteColor]];
        _registerBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        _registerBtn.titleLabel.textColor = [UIColor whiteColor];
        [_registerBtn setTitle:@"登录" forState:UIControlStateNormal];
        [self.view addSubview:_registerBtn];
        [_registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_icon.mas_bottom).offset(20);
            make.centerX.equalTo(_icon);
        }];
        [_registerBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    else if([_enter isEqualToString:@"yes"])
    {
        _icon.hidden = YES;
        _registerBtn.hidden = YES;
        
        _tabView.hidden = NO;
        HMLoginModel *model = [[HMLoginModel alloc] init];
        _iconBtn = [[UIButton alloc] init];
        [_iconBtn setImage:[UIImage imageNamed:@"doctor_defaultphoto_male"] forState:UIControlStateNormal];
        _iconBtn.layer.cornerRadius = 30;
        _iconBtn.layer.masksToBounds = YES;
        
        [self.view addSubview:_iconBtn];
        [_iconBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(80);
            make.left.offset(20);
            make.size.mas_offset(CGSizeMake(60, 60));
        }];
        [_iconBtn addTarget:self action:@selector(clickIconBtn:) forControlEvents:UIControlEventTouchUpInside];
        
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.font = [UIFont systemFontOfSize:18];
        _nameLabel.textColor = [UIColor whiteColor];
        _nameLabel.text = model.name;
        [self.view addSubview:_nameLabel];
        [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_iconBtn);
            make.left.equalTo(_iconBtn.mas_right).offset(20);
        }];
        
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HMLeftMainCell *cell = [tableView dequeueReusableCellWithIdentifier:leftCell forIndexPath:indexPath];
    
    cell.model = _array[indexPath.row];
    
    return cell;
}

- (void)loadData
{
    NSArray *arrayModel = [NSArray arrayWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"Property List.plist" withExtension:nil]];
    
    _array = [NSArray yy_modelArrayWithClass:[HMLeftMainModel class] json:arrayModel];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSString *claName = _arrayVC[indexPath.row];
    Class cls = NSClassFromString(claName);
    
    if ([claName isEqualToString:@"HMSetAndHelpController"])
    {
        HMSetAndHelpController *vc = [[HMSetAndHelpController alloc] init];
        
        vc.delegate = self;
        [self setView:vc Title:_arrayVCTitle[indexPath.row]];
        
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        
    } else {
        
        UITableViewController *vc = [[cls alloc] init];
        
        [self setView:vc Title:_arrayVCTitle[indexPath.row]];
        
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
    
//    [self setView:vc Title:_arrayVCTitle[indexPath.row]];
//    
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)setView:(UIViewController *)viewController  Title:(NSString *)title
{
    viewController.title = title;
    
    UITabBarController *tab = (UITabBarController *)self.drawer.centerViewController;
    UINavigationController *nav = tab.selectedViewController;
    [nav popToRootViewControllerAnimated:NO];
    [self.drawer close];
    nav.navigationBar.tintColor = [UIColor whiteColor];
    viewController.hidesBottomBarWhenPushed = YES;
    [nav pushViewController:viewController animated:NO];
}

- (void)clickBtn:(UIButton *)sender
{

    _dict = [NSMutableDictionary dictionaryWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"IDMessage.plist" withExtension:nil]];
    EnterView *vc = [[EnterView alloc] init];
    vc.delegate = self;
    [self presentViewController:vc animated:YES completion:nil];
    
}

- (void)enterView:(EnterView *)view
{
    
   

     [self setHead];
}


- (void)clickIconBtn:(UIButton *)sender
{
    HMMyMessageController *vc = [[HMMyMessageController alloc] init];
    [self setView:vc Title:@"个人信息"];


}


- (void)setAndHelpController:(HMSetAndHelpController *)vc
{
    
   _dict = nil;
    
    [self setHead];

}

@end
